package com.osgi.desktop.services;

import com.onetwork.core.actions.ButtonTabComponent;
import com.onetwork.core.ui.components.BasicWindow;
import javax.swing.JTabbedPane;

public class TabbedPaneServiceImpl implements TabbedPaneService {
    private JTabbedPane tabbedPane;
    public TabbedPaneServiceImpl(JTabbedPane tabbedPane) {
        this.tabbedPane = tabbedPane;
    }

    @Override
    public void addBasicWindow(BasicWindow basicWindow) {
        int index = this.tabbedPane.indexOfComponent(basicWindow);
        if (index == -1) {
            this.tabbedPane.addTab(basicWindow.getName(), basicWindow.icon(), basicWindow);
            int pos = this.tabbedPane.indexOfComponent(basicWindow);
            this.tabbedPane.setTabComponentAt(pos, new ButtonTabComponent(tabbedPane));
            this.tabbedPane.setIconAt(pos, basicWindow.icon());
        }
        this.tabbedPane.setSelectedComponent(basicWindow);
    }

    @Override
    public void removeBasicWindow(BasicWindow basicWindow) {
        int index = this.tabbedPane.indexOfComponent(basicWindow);
        if (index > -1) this.tabbedPane.remove(basicWindow);
        this.tabbedPane.updateUI();
    }
}
